package com.fourth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fourth.dao.UBaddrDao;
import com.fourth.dao.UBuserInfoDao;
import com.fourth.service.AddrService;
import com.ljx.entity.Addr;
import com.ljx.entity.UserInfo;
import com.ljx.security.util.WebUtils;
import com.ljx.utils.JwtUtil;
import com.ljx.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author C-Q
 * @version 1.1.0
 * @Description: TODO
 * @date 2023/3/3
 */
@Service
public class AddrServiceImpl implements AddrService {
    @Resource
    private UBaddrDao uBaddrDao;

    @Resource
    private UBuserInfoDao uBuserInfoDao;

    @Override
    public Result AddrInsert(Addr addr) {
        //得到token
        String token = WebUtils.getRequest().getHeader("token");
        //解析token
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        //拿到token中携带的用户名
        String username = (String) map.get("username");
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone",username);
        UserInfo userInfo = uBuserInfoDao.selectOne(wrapper);
        System.out.println(userInfo+"6666666000000");
        addr.setUid(userInfo.getId());
        int num=1+(int)(Math.random()*(1000000000-1+1));
        String s = String.valueOf(num);
        addr.setId(s);
        int i = uBaddrDao.AddrInsert(addr);
        if(i>0){
            return new Result(2000,"添加成功",i);
        }
        return new Result(2000,"添加失败");
    }

    @Override
    public Result SelectAll() {
        //得到token
        String token = WebUtils.getRequest().getHeader("token");
        //解析token
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        //拿到token中携带的用户名
        String username = (String) map.get("username");
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("telephone", username);
        UserInfo userInfo = uBuserInfoDao.selectOne(wrapper);

        List<Addr> list = uBaddrDao.SelectAll(userInfo.getId());
        return new Result(2000, "查询成功", list);
    }


}
